<template>
  <div class="order-detail">
    <nav-bar is-back style="background: var(--theme-color); color: white">
      <i slot="left" class="iconfont icon-fanhui"></i>
      <span slot="center">订单详情</span>
      <i slot="right" class="iconfont icon-gengduo"></i>
    </nav-bar>
    <div style="background: var(--theme-color); height: 100px"></div>
    <div class="order-detail-body">
      <van-contact-card
        style="border-radius: 10px"
        type="edit"
        name="张三"
        tel="18792783702"
        :edittable="false"
      />
      <!-- 商品信息 -->
      <div class="goods-list" v-if="!order.id">
        <div class="goods-item" v-for="item in goods" :key="item.goods.id">
          <div class="goods-detail">
            <img class="image" :src="item.goods.show.img" />
            <span class="title">{{ item.goods.title }}</span>
            <div style="width: 50px; text-align: center">
              <span>{{ item.goods.price | priceFilter }}</span>
              <div>x{{ item.num }}</div>
            </div>
          </div>
          <div class="price">
            <span>商品金额</span>
            <span>
              {{ (item.goods.price * item.num) | priceFilter }}
            </span>
          </div>
        </div>
      </div>
      <!-- 订单信息 -->
      <div class="goods-list" v-else>
        <div class="goods-item" v-for="item in order.orderDesc" :key="item.id">
          <div class="goods-detail">
            <img class="image" :src="item.show" />
            <span class="title">{{ item.title }}</span>
            <div style="width: 50px; text-align: center">
              <span>{{ item.price | priceFilter }}</span>
              <div>x{{ item.num }}</div>
            </div>
          </div>
          <div class="price">
            <span>商品金额</span>
            <span>
              {{ (item.price * item.num) | priceFilter }}
            </span>
          </div>
        </div>
      </div>
      <div class="total-price">
        实付金额：{{ order.totalPrice || totalPrice | priceFilter }}
      </div>
      <div class="order-desc" v-if="order.id">
        <p>订单详情</p>
        <div style="display: flex; align-items: center; font-size: 13px">
          <div>
            <p>订单编号：{{ order.id }}</p>
            <p>下单时间：{{ order.createDatetime | timeFilter }}</p>
          </div>
          <span style="width: 60px; text-align: right">复制</span>
        </div>
      </div>
    </div>
    <div class="submit-bar">
      <van-button class="btn" plain size="small" round>联系客服 </van-button>
      <van-button
        class="btn"
        color="#a1a4aa"
        plain
        size="small"
        v-if="order.status == 0"
        @click="cancel(order.id)"
        round
      >
        取消
      </van-button>
      <van-button
        class="btn"
        color="red"
        size="small"
        v-if="order.status == 0"
        @click="pay(order.id)"
        round
      >
        支付
      </van-button>

      <van-button
        class="btn"
        color="red"
        plain
        size="small"
        v-if="order.status == 1 || order.status == 2"
        @click="del(order.id)"
        round
      >
        删除
      </van-button>

      <van-button
        class="btn"
        plain
        size="small"
        color="var(--theme-color)"
        round
        @click="handleSaveOrder()"
        v-if="!order.id"
      >
        添加订单
      </van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import dayjs from 'dayjs'
import { toast } from '@/utils'
import {
  queryGoodsById,
  saveOrder,
  deleteCart,
  queryOrderById,
  del,
  pay,
  cancel
} from '../../api'

import NavBar from '../../components/nav-bar/nav-bar.vue'
export default {
  data: () => ({
    goods: [], //商品
    info: [], //[gid，num]
    cids: [], //购物车id
    order: {} //订单信息
  }),
  components: { NavBar },
  mounted() {
    //当订单已经存在id

    const { id: oid } = this.$route.params
    if (oid) {
      //查询订单信息
      this.queryOrder(oid)
    } else {
      //查询的是商品的信息
      const { info, id } = this.$route.query
      this.info = JSON.parse(info)
      if (id) {
        this.cids = JSON.parse(id)
      }

      this.queryGoods()
    }
  },
  computed: {
    ...mapState({
      uid: (state) => state.userInfo.id
    }),

    totalPrice() {
      return this.goods.reduce(
        (pre, item) => pre + item.goods.price * item.num,
        0
      )
    }
  },
  methods: {
    async queryGoods() {
      for (const item of this.info) {
        const { data } = await queryGoodsById({ id: item.gid })
        this.goods.push({ goods: data, num: item.num })
      }
      console.log(this.goods)
    },
    //添加订单
    async handleSaveOrder() {
      const result = await saveOrder({
        uid: this.uid,
        address: '海南省三亚市xx区',
        info: this.info
      })
      console.log(result)
      //如果是从购物车页面过来的话需要删除购物车中的记录
      if (result && this.cids.length > 0) {
        await deleteCart({
          id: this.cids
        })
      }
      this.$router.replace('/order')
    },
    //查询订单信息
    async queryOrder(id) {
      const { data } = await queryOrderById(id)
      this.order = data
      console.log(data)
    },
    //删除订单
    async del(id) {
      const { data } = await del({ id })
      toast.success(data)
      this.$router.replace('/order/0')
    },
    //支付
    async pay(id) {
      const { data } = await pay({ id })
      toast.success(data)
      this.$router.replace('/order/1')
    },
    //取消订单
    async cancel(id) {
      const { data } = await cancel({ id })
      toast.success(data)
      this.$router.replace('/order/2')
    }
  },

  filters: {
    priceFilter(price) {
      return '￥' + price.toFixed(2)
    },
    timeFilter(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang="less">
.order-detail {
  background: #f6f6f6;
  height: 100vh;
}
.order-detail-body {
  padding: 0 15px;
  margin-top: -50px;
}
.goods-list {
  margin-top: 10px;
}
.goods-item {
  background: white;
  .goods-detail {
    padding: 15px;
    display: flex;
    align-items: center;
    height: 90px;
    font-size: 13px;
    .image {
      width: 60px;
      height: 60px;
      border-radius: 10px;
    }
    .title {
      flex: 1;
      color: #090c19;
      text-overflow: ellipsis;
      overflow: hidden;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      line-height: 30px;
      padding: 0 15px;
    }
  }
  .price {
    display: flex;
    justify-content: space-between;
    margin: 0 15px;
    font-size: 13px;
    border-bottom: 1px solid #a1a4aa20;
    line-height: 35px;
  }
}
.total-price {
  background: white;
  line-height: 50px;
  text-align: right;
  padding-right: 15px;
  font-weight: 700;
}
.submit-bar {
  background: white;
  height: 52px;
  position: fixed;
  bottom: 0;
  z-index: 9;
  width: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 15px;
  .btn {
    margin: 0 5px;
  }
}
.order-desc {
  background: white;
  margin-top: 15px;
  border-radius: 10px;
  padding: 15px;
  > p {
    font-weight: 700;
  }
}
</style>
